message.js ➔ describe(ꞌTest Email APIꞌ)   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 58
rs 9.639

1 Function

Rating   Name   Duplication   Size   Complexity  
B message.js ➔ ... ➔ it(ꞌit should send an emailꞌ) 0 28 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
var chai = require('chai')
2
var server = require('../../app')
3
/* eslint-disable no-unused-vars */
4
var should = chai.should()
5
var nock = require('nock')
6
var nodemailer = require('nodemailer')
7
var sinon = require('sinon')
8
var expect = require('chai').expect
9
var prefix = '/api/messages'
10
11
describe('Test Email API', function () {
12
  it('it should send an email', function (done) {
13
    var transportStub = {
14
      sendMail: function (options) {
0 ignored issues
show
Unused Code introduced by
The parameter options is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
15
        return new Promise((resolve, reject) => { resolve(true, false) })
0 ignored issues
show
Unused Code introduced by
The parameter reject is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
16
      }
17
    }
18
19
    var sendMailSpy = sinon.spy(transportStub, 'sendMail')
0 ignored issues
show
Unused Code introduced by
The variable sendMailSpy seems to be never used. Consider removing it.
Loading history...
20
    var mailerStub = sinon.stub(nodemailer, 'createTransport').returns(transportStub)
21
22
    var mail = {
23
      to: '[email protected]',
24
      subject: 'Hello ✔',
25
      text: 'Hello world 🐴',
26
      html: '<b>Hello world 🐴</b>'
27
    }
28
    chai.request(server)
29
      .post(prefix + '/email')
30
      .send(mail)
31
      .end(function (err, res) {
32
        should.not.exist(err)
33
        res.should.have.status(200)
34
        res.body.should.be.a('object')
35
        res.body.should.have.property('message').eql('Message sent')
36
        mailerStub.restore()
37
        done()
38
      })
39
  })
40
41
  it('it should send an email', function (done) {
42
    var transportStub = {
43
      sendMail: function (options) {
0 ignored issues
show
Unused Code introduced by
The parameter options is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
44
        return new Promise((resolve, reject) => { reject(new Error('Something went wrong with mail service')) })
45
      }
46
    }
47
    var sendMailSpy = sinon.spy(transportStub, 'sendMail')
0 ignored issues
show
Unused Code introduced by
The variable sendMailSpy seems to be never used. Consider removing it.
Loading history...
48
    var mailerStub = sinon.stub(nodemailer, 'createTransport').returns(transportStub)
49
50
    var mail = {
51
      to: '[email protected]',
52
      subject: 'Hello ✔',
53
      text: 'Hello world 🐴',
54
      html: '<b>Hello world 🐴</b>'
55
    }
56
    chai.request(server)
57
      .post(prefix + '/email')
58
      .send(mail)
59
      .end(function (err, res) {
60
        //should.exist(err)
61
        res.should.have.status(404)
62
        res.body.should.be.a('object')
63
        err.should.have.property('message').eql('Not Found')
64
        mailerStub.restore()
65
        done()
66
      })
67
  })
68
})
69
70
describe('Test SMS API', function () {
71
  xit('it should send an sms', function (done) {
72
    nock('https://www.my-cool-sms.com')
73
      .get('/api-socket.php')
74
      .reply(200, {
75
        'success': true,
76
        'smsid': 'ce184cc0a6d1714d1ac763f4fe89f521',
77
        'body': 'Have a nice day!',
78
        'bodyucs2': '0048006100760065002000610020006E00690063006500200064',
79
        'bodygsm7': '486176652061206E6963652064617921',
80
        'number': '+491234567890',
81
        'senderid': '+449876543210',
82
        'senderidenabled': true,
83
        'unicode': false,
84
        'numchars': 321,
85
        'escapenumchars': 0,
86
        'smscount': 3,
87
        'charge': 0.112,
88
        'balance': 752.121,
89
        'countrycode': 'IE',
90
        'prefix': '+353',
91
        'timestamp': '2017-04-02T22:27:22-07:00',
92
        'callbackurl': 'https://www.groganburners.ie/api/sms/callback'
93
      })
94
95
    var mail = {
96
      number: '+353873791474',
97
      message: 'Hello'
98
    }
99
    chai.request(server)
100
      .post(prefix + '/sms')
101
      .send(mail)
102
      .end(function (err, res) {
103
        should.not.exist(err)
104
        res.should.have.status(200)
105
        res.body.should.be.a('object')
106
        res.body.should.have.property('message').eql('Message sent')
107
        nock.cleanAll()
108
        done()
109
      })
110
  })
111
112
  it('it should fail to send an sms to invalid number', function (done) {
113
    nock('https://www.my-cool-sms.com')
114
      .get('/api-socket.php')
115
      .reply(200, {
116
        success: false,
117
        errorcode: '210',
118
        description: 'The number seems to be invalid'
119
      })
120
    var mail = {
121
      number: '+35386',
122
      message: 'Hello'
123
    }
124
    chai.request(server)
125
      .post(prefix + '/sms')
126
      .send(mail)
127
      .end(function (err, res) {
128
        should.exist(err)
129
        res.should.have.status(404)
130
        res.body.should.be.a('object')
131
        err.should.have.property('message').eql('Not Found')
132
        nock.cleanAll()
133
        done()
134
      })
135
  })
136
})
137